package tingshu.service;


import tingshu.data_object.TidToCountDO;
import tingshu.data_object.TrackDO;
import tingshu.repository.RelationRepo;
import tingshu.repository.TrackRepo;
import tingshu.util.Log;
import tingshu.view_object.ListTrackView;
import tingshu.view_object.PaginationView;
import tingshu.view_object.StudioTrackListView;
import tingshu.view_object.UserVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TrackService {
    TrackRepo trackRepo = new TrackRepo();
    RelationRepo relationRepo = new RelationRepo();
    public StudioTrackListView listOfUser(UserVO currentUser, int page) {
        StudioTrackListView resultView = new StudioTrackListView();
        resultView.currentUser = currentUser;

        int countPerPage = 5;
        Log.println("每页个数: " + countPerPage);
        int count = trackRepo.selectCountByUid(currentUser.uid);
        Log.println("属于当前用户的音频共有 " + count + " 个");
        if (count == 0) {
            resultView.trackList = new ArrayList<>();
            resultView.pagination = new PaginationView();
            resultView.pagination.countPerPage = countPerPage;
            resultView.pagination.totalPage = 0;
            resultView.pagination.currentPage = 1;
            return resultView;
        }

        int totalPage = count / countPerPage;
        if (count % countPerPage > 0) {
            totalPage++;
        }
        Log.println("共有页数: " + totalPage);

        if (page <= 0) {
            Log.println("用户输入页数" + page + "页面信息小于最小值，调整为 1");
            page = 1;
        }
        if (page > totalPage) {
            Log.println("用户输入页数" + page + "页面信息大于最大值，调整为最大值");
            page = totalPage;
        }

        int offset = 5 * (page - 1);
        List<TrackDO> list = trackRepo.selectListByUidLimitOffset(currentUser.uid, countPerPage, offset);

        if (list != null) {
            List<Integer> tidList = new ArrayList<>();
            for (TrackDO trackDO : list) {
                tidList.add(trackDO.tid);
            }
            List<TidToCountDO> tidToCountDOList = relationRepo.selectCountByTidListGroupByTidList(tidList);
            Map<Integer,Integer> tidToRefCountMap = new HashMap<>();
            for (TidToCountDO tidToCountDO : tidToCountDOList) {
                tidToRefCountMap.put(tidToCountDO.tid, tidToCountDO.refCount);
            }

            List<ListTrackView> retList = new ArrayList<>();
            for (TrackDO trackDO : list) {
                retList.add(new ListTrackView(trackDO, tidToRefCountMap.getOrDefault(trackDO.tid,0 )));
            }
            resultView.trackList = retList;
        } else {
            Log.println("查询结果为空");
            resultView.trackList = null;
        }
        PaginationView paginationView = new PaginationView();
        paginationView.currentPage = page;
        paginationView.countPerPage = countPerPage;
        paginationView.totalPage = totalPage;
        resultView.pagination = paginationView;

        return resultView;
    }
}
